﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;
using DAL;

namespace QLTTTH
{
    public partial class UCChungChi : DevExpress.XtraEditors.XtraUserControl
    {
        public UCChungChi()
        {
            InitializeComponent();
        }

        private void UCChungChi_Load(object sender, EventArgs e)
        {
            LoadChungChi();
            grvDSChungChi.OptionsSelection.MultiSelect = true;
        }
        private void LoadChungChi()
        {
            var aResult = ChungChiManager.LayDanhSachChungChi();
            if (aResult.Success)
                gridDanhSach.DataSource = aResult.Result;
                //grvDSChungChi.DataSource = aResult.Result;
        }

        private void btnTimKiem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            grvDSChungChi.ShowFindPanel();
        }



        private void btnThem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmThongTinChungChi frm = new frmThongTinChungChi();
            frm.sendData = new frmThongTinChungChi.SendData(ThemChungChi);
            frm.ShowDialog();
        }
        private void ThemChungChi(ChungChi chungchi)
        {
            if (ChungChiManager.ThemChungChi(chungchi).Success)
            {
                LoadChungChi();
            }
        }




        private void btnXemChiTiet_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmThongTinChungChi frm = new frmThongTinChungChi();
            frm.FillControl(LayChungChi_GrvDanhSach());
            frm.btnLuu.Enabled = false;
            frm.ShowDialog();
        }
        private ChungChi LayChungChi_GrvDanhSach()
        {
            var chngchi = new ChungChi();
            chngchi.MaChungChi = int.Parse(grvDSChungChi.GetFocusedRowCellValue("MaChungChi").ToString());
            chngchi.MaMH = int.Parse(grvDSChungChi.GetFocusedRowCellValue("MaMH").ToString());
            chngchi.ThoiHan = grvDSChungChi.GetFocusedRowCellValue("ThoiHan").ToString();
            chngchi.TenChungChi = grvDSChungChi.GetFocusedRowCellValue("TenChungChi").ToString();
            chngchi.NoiCap = grvDSChungChi.GetFocusedRowCellValue("NoiCap").ToString();
            return chngchi;
        }



        private void btnXoa_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (XtraMessageBox.Show("Bạn chắc chắn xóa chứng chỉ này", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                int maChungChi;

                for (int i = 0; i < grvDSChungChi.RowCount; i++)
                {
                    if (grvDSChungChi.IsRowSelected(i))
                    {
                        maChungChi = int.Parse(grvDSChungChi.GetRowCellDisplayText(i, "MaChungChi").ToString());
                        ChungChiManager.XoaChungChi(maChungChi);
                    }
                }

                grvDSChungChi.DeleteSelectedRows();

            }
        }




        private void CapNhatChungChi(ChungChi chungch)
        {
            ChungChiManager.CapNhatChungChi(chungch);
            grvDSChungChi.SetFocusedRowCellValue("TenChungChi", chungch.TenChungChi);
            grvDSChungChi.SetFocusedRowCellValue("NoiCap", chungch.NoiCap);
            grvDSChungChi.SetFocusedRowCellValue("ThoiHan", chungch.ThoiHan);
            grvDSChungChi.SetFocusedRowCellValue("MaMH", chungch.MaMH);
            //grvDSChungChi.SetFocusedRowCellValue("TenChungChi", chungch.TenChungChi);
        }

        private void btnCapNhat_LinkClicked_1(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmThongTinChungChi frm = new frmThongTinChungChi();
            frm.FillControl(LayChungChi_GrvDanhSach());
            frm.sendData = new frmThongTinChungChi.SendData(CapNhatChungChi);
            frm.ShowDialog();
        }
    }
}
